package com.zy.design.pattern.single;

/**
 * @author zhaoyi
 * @date 2021/3/29 15:27
 * 单例模式 - 懒汉式 - 双检查
 */
public class DoubleCheckSingle {

    private static volatile DoubleCheckSingle INSTANCE;

    //私有化构造函数
    private DoubleCheckSingle(){
        System.out.println("!!!!!单例对象 DoubleCheckSingle 被创建了");
    }

    public static DoubleCheckSingle getInstance(){
        //不加锁判断,提高并发度
        if (INSTANCE == null){
            //静态方法 类锁
            synchronized (DoubleCheckSingle.class){
                //二次检查,防止并发多个同时通过第一个检查.
                if (INSTANCE == null){
                    INSTANCE = new DoubleCheckSingle();
                }
            }
        }
        return INSTANCE;
    }


    public void sayHello() {
        System.out.println(Thread.currentThread().getName() + " hello " + this);
    }





}
